﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using C.Common;
using System.Linq;

namespace Gfw.Web.Common
{
    public static class DescriptionEnumList
    {
        public static IEnumerable<SelectListItem> ToDropdownList(this Type type)
        {
            List<SelectListItem> li = new List<SelectListItem>();
            foreach (var s in Enum.GetNames(type))
            {
                var attribute = type.GetField(s).DescriptionAttribute();
                if (attribute != null)
                {
                    li.Add(new SelectListItem
                    {
                        Value = ((int)Enum.Parse(type, s)).ToString(),
                        Text = attribute.Name
                    });
                }
            }
            return li;
        }

        public static string GetDescriptionName(this int enumSort, Type enumType)
        {

            var enumName = Enum.GetName(enumType, enumSort);
            var attribute =  enumType.GetField(enumName).DescriptionAttribute();
            if (attribute != null)
                {
                return attribute.Name;
            }

            return enumName;
        }
    }
}